#ifndef PTZCONTROL_H
#define PTZCONTROL_H

#include <QWidget>
#include <QPushButton>
#include <QVBoxLayout>
#include <QLabel>
#include <QPainter>
#include <QPropertyAnimation>
#include <cmath>
#include <QJsonObject>
#include <QCoreApplication>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QCryptographicHash>
#include <QDateTime>

class DirectionButton : public QPushButton {
    Q_OBJECT
    Q_PROPERTY(qreal pressedScale READ pressedScale WRITE setPressedScale)
public:
    explicit DirectionButton(int direction, QWidget *parent = nullptr)
        : QPushButton(parent), m_direction(direction), m_pressedScale(1.0) {
        setFixedSize(40, 40);
        setCursor(Qt::PointingHandCursor);
    }

    qreal pressedScale() const { return m_pressedScale; }
    void setPressedScale(qreal scale) {
        m_pressedScale = scale;
        update();
    }

protected:
    void paintEvent(QPaintEvent *) override {
        QPainter painter(this);
        painter.setRenderHint(QPainter::Antialiasing);

        // 绘制圆形背景
        painter.setPen(Qt::NoPen);
        painter.setBrush(isDown() ? QColor(0x88, 0x88, 0x88) : QColor(0xAA, 0xAA, 0xAA));
        painter.drawEllipse(rect().adjusted(1, 1, -1, -1));

        // 绘制方向箭头
        painter.setBrush(QColor(0x2D, 0x2D, 0x2D));

        const int centerX = width() / 2;
        const int centerY = height() / 2;
        const int size = 12;

        // 根据方向绘制箭头
        QPolygon arrow;
        switch(m_direction) {
        case 0: // 上
            arrow << QPoint(centerX, centerY - size/2)
                  << QPoint(centerX - size/2, centerY + size/2)
                  << QPoint(centerX + size/2, centerY + size/2);
            break;
        case 1: // 右上
            arrow << QPoint(centerX + size/2, centerY + size/2)
                  << QPoint(centerX - size/2, centerY - size/2)
                  << QPoint(centerX + size/2, centerY - size/2);
            break;
        case 2: // 右
            arrow << QPoint(centerX + size/2, centerY)
                  << QPoint(centerX - size/2, centerY - size/2)
                  << QPoint(centerX - size/2, centerY + size/2);
            break;
        case 3: // 右下
            arrow << QPoint(centerX + size/2, centerY - size/2)
                  << QPoint(centerX - size/2, centerY + size/2)
                  << QPoint(centerX + size/2, centerY + size/2);
            break;
        case 4: // 下
            arrow << QPoint(centerX, centerY + size/2)
                  << QPoint(centerX - size/2, centerY - size/2)
                  << QPoint(centerX + size/2, centerY - size/2);
            break;
        case 5: // 左下
            arrow << QPoint(centerX - size/2, centerY - size/2)
                  << QPoint(centerX + size/2, centerY + size/2)
                  << QPoint(centerX - size/2, centerY + size/2);
            break;
        case 6: // 左
            arrow << QPoint(centerX - size/2, centerY)
                  << QPoint(centerX + size/2, centerY - size/2)
                  << QPoint(centerX + size/2, centerY + size/2);
            break;
        case 7: // 左上
            arrow << QPoint(centerX - size/2, centerY - size/2)
                  << QPoint(centerX + size/2, centerY - size/2)
                  << QPoint(centerX - size/2, centerY + size/2);
            break;
        }

        painter.drawPolygon(arrow);
    }

    void mousePressEvent(QMouseEvent *e) override {
        QPushButton::mousePressEvent(e);

        // 按下动画效果
        QPropertyAnimation *anim = new QPropertyAnimation(this, "pressedScale");
        anim->setDuration(100);
        anim->setEasingCurve(QEasingCurve::OutQuad);
        anim->setStartValue(0.9);
        anim->setEndValue(1.0);
        anim->start(QAbstractAnimation::DeleteWhenStopped);
    }

private:
    int m_direction;
    qreal m_pressedScale;
};

class PTZControl : public QWidget {
    Q_OBJECT
public:
    explicit PTZControl(QWidget *parent = nullptr) : QWidget(parent) {
        setupUI();
        applyStyles();
    }
public:
    void sendRequest(const char* dir);
private:
    QNetworkAccessManager manager;
    QNetworkRequest request;

private:
    void setupUI() {
        // 主布局
        QVBoxLayout *mainLayout = new QVBoxLayout(this);
        mainLayout->setAlignment(Qt::AlignCenter);
        mainLayout->setSpacing(15);
        mainLayout->setContentsMargins(0, 20, 0, 20);

        // 标题
        QLabel *title = new QLabel("云台控制");
        title->setAlignment(Qt::AlignCenter);
        mainLayout->addWidget(title);

        // 创建控制面板容器 - 设置为透明
        QWidget *panelContainer = new QWidget(this);
        panelContainer->setFixedSize(200, 200);
        panelContainer->setStyleSheet("background-color: transparent;");
        mainLayout->addWidget(panelContainer, 0, Qt::AlignCenter);

        // 创建方向按钮
        DirectionButton *btnUp = new DirectionButton(0, panelContainer);
        DirectionButton *btnUpRight = new DirectionButton(1, panelContainer);
        DirectionButton *btnRight = new DirectionButton(2, panelContainer);
        DirectionButton *btnDownRight = new DirectionButton(3, panelContainer);
        DirectionButton *btnDown = new DirectionButton(4, panelContainer);
        DirectionButton *btnDownLeft = new DirectionButton(5, panelContainer);
        DirectionButton *btnLeft = new DirectionButton(6, panelContainer);
        DirectionButton *btnUpLeft = new DirectionButton(7, panelContainer);

        // 中心按钮
        QPushButton *btnCenter = new QPushButton(panelContainer);
        btnCenter->setObjectName("centerBtn");
        btnCenter->setFixedSize(60, 60);
        btnCenter->setCursor(Qt::PointingHandCursor);

        // 设置循环箭头图标
        QPixmap cycleIcon(40, 40);
        cycleIcon.fill(Qt::transparent);
        QPainter painter(&cycleIcon);
        painter.setRenderHint(QPainter::Antialiasing);
        painter.setPen(QPen(QColor(0xAA, 0xAA, 0xAA), 3));

        // 绘制两个同心半圆箭头
        painter.drawArc(5, 5, 30, 30, 30 * 16, 300 * 16);  // 外圈
        painter.drawArc(10, 10, 20, 20, 30 * 16, 300 * 16); // 内圈
        painter.end();

        btnCenter->setIcon(QIcon(cycleIcon));
        btnCenter->setIconSize(QSize(40, 40));

        // 精确计算按钮位置 - 使用三角函数计算圆周位置
        const double radius = 70.0; // 圆周半径
        const double centerX = panelContainer->width() / 2.0; // 圆心X坐标
        const double centerY = panelContainer->height() / 2.0; // 圆心Y坐标
        const double verticalOffset = 10.0; // 垂直偏移量，使按钮向下移动

        // 计算每个按钮的位置（从正上方开始顺时针）
        // 0: 上 (270度)
        btnUp->move(centerX - 20 + radius * cos(-M_PI_2),
                    centerY - 20 + radius * sin(-M_PI_2) + verticalOffset);

        // 1: 右上 (315度)
        btnUpRight->move(centerX - 20 + radius * cos(-M_PI_4),
                         centerY - 20 + radius * sin(-M_PI_4) + verticalOffset);

        // 2: 右 (0度)
        btnRight->move(centerX - 20 + radius * cos(0),
                       centerY - 20 + radius * sin(0) + verticalOffset);

        // 3: 右下 (45度)
        btnDownRight->move(centerX - 20 + radius * cos(M_PI_4),
                           centerY - 20 + radius * sin(M_PI_4) + verticalOffset);

        // 4: 下 (90度)
        btnDown->move(centerX - 20 + radius * cos(M_PI_2),
                      centerY - 20 + radius * sin(M_PI_2) + verticalOffset);

        // 5: 左下 (135度)
        btnDownLeft->move(centerX - 20 + radius * cos(3*M_PI/4),
                          centerY - 20 + radius * sin(3*M_PI/4) + verticalOffset);

        // 6: 左 (180度)
        btnLeft->move(centerX - 20 + radius * cos(M_PI),
                      centerY - 20 + radius * sin(M_PI) + verticalOffset);

        // 7: 左上 (225度)
        btnUpLeft->move(centerX - 20 + radius * cos(5*M_PI/4),
                        centerY - 20 + radius * sin(5*M_PI/4) + verticalOffset);

        // 中心按钮位置 - 居中放置
        btnCenter->move(centerX - 30, centerY - 30 + verticalOffset);

        // 连接信号槽
        connect(btnCenter, &QPushButton::pressed, this, &PTZControl::onCenterClicked);
        connect(btnUp, &QPushButton::pressed, this, [this]{  directionClicked(0); });
        connect(btnUpRight, &QPushButton::pressed, this, [this]{  directionClicked(1); });
        connect(btnRight, &QPushButton::pressed, this, [this]{  directionClicked(2); });
        connect(btnDownRight, &QPushButton::pressed, this, [this]{  directionClicked(3); });
        connect(btnDown, &QPushButton::pressed, this, [this]{  directionClicked(4); });
        connect(btnDownLeft, &QPushButton::pressed, this, [this]{  directionClicked(5); });
        connect(btnLeft, &QPushButton::pressed, this, [this]{  directionClicked(6); });
        connect(btnUpLeft, &QPushButton::pressed, this, [this]{  directionClicked(7); });

        connect(btnUp, &QPushButton::released, this, [this]{  directionReleased(); });
        connect(btnUpRight, &QPushButton::released, this, [this]{  directionReleased(); });
        connect(btnRight, &QPushButton::released, this, [this]{  directionReleased(); });
        connect(btnDownRight, &QPushButton::released, this, [this]{  directionReleased(); });
        connect(btnDown, &QPushButton::released, this, [this]{  directionReleased(); });
        connect(btnDownLeft, &QPushButton::released, this, [this]{  directionReleased(); });
        connect(btnLeft, &QPushButton::released, this, [this]{  directionReleased(); });
        connect(btnUpLeft, &QPushButton::released, this, [this]{  directionReleased(); });
    }

    void applyStyles() {
        // 全局样式
        setStyleSheet(
            "background-color: white;" // 白色背景
            "QLabel {"
            "   color: black;" // 黑色文字
            "   font-size: 18px;"
            "   font-weight: bold;"
            "   margin-bottom: 10px;"
            "}"
            "QPushButton#centerBtn {"
            "   background-color: #1A1A1A;"
            "   border-radius: 30px;"
            "   border: 2px solid #333333;"
            "}"
            "QPushButton#centerBtn:hover {"
            "   background-color: #222222;"
            "}"
            "QPushButton#centerBtn:pressed {"
            "   background-color: #282828;"
            "}"
        );
    }

protected:
    void paintEvent(QPaintEvent *) override {
        QPainter painter(this);
        painter.setRenderHint(QPainter::Antialiasing);

        // 计算标题高度
        int titleHeight = 40;

        // 计算面板容器位置
        int panelSize = 200;
        int panelX = (width() - panelSize) / 2;
        int panelY = (height() - panelSize - titleHeight) / 2 + titleHeight + 5; // 下移5px

        // 绘制圆形控制面板背景
        painter.setPen(Qt::NoPen);
        painter.setBrush(QColor(0x2D, 0x2D, 0x2D));
        painter.drawEllipse(panelX, panelY, panelSize, panelSize);

        // 绘制中心圆环 - 实心圆
        painter.setPen(Qt::NoPen);
        painter.setBrush(QColor(0x1A, 0x1A, 0x1A));
        painter.drawEllipse(panelX + 60, panelY + 60, 80, 80);
    }
public slots:
    void directionClicked(int direction);
    void directionReleased();
    void centerButtonClicked();

private slots:
    void onCenterClicked() {
         centerButtonClicked();
    }
};
#endif // PTZCONTROL_H
